package com.wthink.newMain.cameraSettings.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CameraSettingsViewmodel : ViewModel() {

    val cameraList = MutableLiveData(emptyList<String>())

    val spinnerList = MutableLiveData(emptyList<String>())

    val list = arrayListOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")

    fun start(){
        spinnerList.postValue(arrayListOf("选择要调整的频道"))
        getConfig()
    }

    fun setSpinnerList() {
        spinnerList.postValue(list)
    }

    fun getConfig() {
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.getCameraConfig()
            if (ret is Result.Success) {
                cameraList.postValue(ret.data.cameraList)
                playCamera(ret.data.cameraList)
            }
        }
    }

    fun setConfig(payload:String){
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.setCameraSettingConfig(payload)
            if (ret is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "摄像头配置通道成功")
                }
                getConfig()
            }
        }
    }

    fun playCamera(list: List<String>) {
        viewModelScope.launch(Dispatchers.IO) {
            DevService.setCamera(list)
        }
    }
}